package com.coolw.test.compare.diff;

import org.dromara.hutool.core.map.MapUtil;

import java.util.Map;

/**
 * 字段差异信息
 *
 * @author coolw
 * @date 2024/10/9 17:54
 */
public class FieldDiffInfo {

    /**
     * 字段差异信息Map[字段名称:不一致的值]
     */
    private final Map<String, String[]> diffMap = MapUtil.newSafeConcurrentHashMap();

    /**
     * 添加字段差异信息
     *
     * @param fieldName 字段名称
     * @param value1    值1
     * @param value2    值2
     */
    public void addDiff(String fieldName, String value1, String value2) {
        diffMap.put(fieldName, new String[]{value1, value2});
    }

    /**
     * 获取字段差异信息
     *
     * @return 字段差异信息Map
     */
    public Map<String, String[]> getDiff() {
        return diffMap;
    }

    /**
     * 获取字段差异明细信息
     *
     * @return 字段差异明细信息
     */
    public String getDiffDetail() {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String[]> entry : diffMap.entrySet()) {
            String fieldName = entry.getKey();
            String[] values = entry.getValue();
            sb.append("Field: ").append(fieldName)
                    .append(",diff: ").append(values[0]).append(" vs ").append(values[1])
                    .append("\n");
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        return getDiffDetail();
    }
}
